<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>供货详情</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" type="text/css" href="../../common/bootstrap/css/bootstrap.css" media="all">
	<link rel="stylesheet" type="text/css" href="../../common/layui/css/layui.css" media="all">
	<link rel="stylesheet" type="text/css" href="../../common/global.css" media="all">
	<link rel="stylesheet" type="text/css" href="../../css/customer_list.css" media="all">
	<link rel="stylesheet" type="text/css" href="../../common/module/formSelects/formSelects-v4.css" media="all">
</head>
<style>
	.layui-table-cell {
		white-space: normal !important; /* 允许多行显示 */
		word-break: break-all;          /* 长单词或 URL 地址换行 */
	}

	.layui-table-body .layui-none {
		line-height: 26px;
		padding: 15px 15px 15px 208px;
		color: #999;
	}

	tbody tr .layui-table-cell {
		height: auto !important;
	}
	.layui-form-item .layui-input-inline {
		font-size: 14px;
		width: 250px;
		margin-right: 10px;
	}

	.main_content_wrap{
		margin-top: 0px;
	}
	.detail_wrap{
		padding: 0 10px;
	}

	.must{
		color: red;
	}

	.order_massage{
		padding:0 15px;
	}
	.order_massage li{
		overflow: hidden;
		padding: 3px 0;
		font-size: 14px;
		color: #000;
	}

	.order_massage li:last-child{
		border-bottom: none;
	}
	.order_massage_title{
		float: left;
		width: 25%;
	}
	.order_address_title{
		float: left;
		width: 100%;
	}
	.layui-input-block {
		margin-left: 95px;
		min-height: 36px;
	}
	.peijian_totall{
		color: #a4a3a3;font-size: 13px;font-weight: bold
	}
	.file-box{
		border: #ededed 1px solid;
		float: left;
		margin-top: 5px;
		font-size: 16px;
	}
	.layui-form-item .file-info {
		float: left;
		margin-right: 10px;
	}
	.file-content {
		float: left;
		padding: 10px;
	}
	.file-box .layui-input-block {
		margin-left: 10px;
	}
	.layui-icon {
		font-size: 18px;
	}
</style>
<body>
<!-- 添加车辆和司机 -->
<div class="layui-fluid content_box">
	<div class="main_content_wrap">
		<form class="layui-form add_form" lay-filter="addForm" id="addForm" action="">
			<p class="add_title">基础信息</p>
			<div class="layui-card-body" id="orderView"></div>
			<p class="add_title">零件清单</p>
			<div class="detail_wrap">
				<table class="layui-table max_w" id="infoTable" lay-filter="infoTable"></table>
			</div>
			<!-- 按钮 -->
			<div class="layui-row layui-col-space5 detail_wrap">
				<!-- 卡片数据集合 -->
				<div class="layui-col-sm6 layui-col-md8">
					<p class="add_title">付款记录</p>
					<table class="layui-table max_w" id="moneyLog" lay-filter="moneyLog"></table>
				</div>
				<!--<div class="layui-col-sm6 layui-col-md4">
					<p class="add_title">发货记录</p>
					<table class="layui-table max_w" id="sendGoodsLog" lay-filter="sendGoodsLog"></table>
				</div>-->
				<div class="layui-col-sm6 layui-col-md4">
					<p class="add_title">开票记录</p>
					<table class="layui-table max_w" id="invoiceLog" lay-filter="invoiceLog"></table>
				</div>
			</div>
			<div class="layui-form-item text-center" style="margin-top: 20px">
				<button class="layui-btn" id="createFile">生成供货凭证</button>
			</div>
		</form>
	</div>
</div>


<script id="orderDetail" type="text/html">
	<ul class='order_massage'>
		<li>
			<span class="order_massage_title">询价来源：{{d.bus_from_company_name?d.bus_from_company_name:''}}
				<a class="layui-btn-a copyInfo" style="background:none;margin-left: 3px" data-name="{{d.bus_from_company_name}}">
					<img src="../../img/copy.png" style="width: 25px;"/>
				</a>
			</span>
			<span class="order_massage_title">询价单号：{{d.bus_inquiry_no}}</span>
			<span class="order_massage_title">询价人：{{d.bus_user_name?d.bus_user_name:''}}
				{{# if(d.bus_user_name){ }}
				<a class="layui-btn-a copyInfo" style="background:none;margin-left: 3px" data-name="{{d.bus_user_name}}">
					<img src="../../img/copy.png" style="width: 25px;"/>
				</a>
				{{# } }}
			</span>
			<span class="order_massage_title">询价人电话：{{d.bus_user_phone?d.bus_user_phone:''}}
				{{# if(d.bus_user_phone){ }}
				<a class="layui-btn-a copyInfo" style="background:none;margin-left: 3px" data-name="{{d.bus_user_phone}}">
					<img src="../../img/copy.png" style="width: 25px;"/>
				</a>
				{{# } }}
			</span>

		</li>
		<li>
			<span class="order_massage_title">车牌号码：{{d.vehicle_license_num?d.vehicle_license_num:''}}
				{{# if(d.vehicle_license_num){ }}
				<a class="layui-btn-a copyInfo" style="background:none;margin-left: 3px" data-name="{{d.vehicle_license_num}}">
					<img src="../../img/copy.png" style="width: 25px;"/>
				</a>
				{{# } }}
			</span>
			<span class="order_massage_title">车架号：{{d.vehicle_frame_no?d.vehicle_frame_no:''}}
				{{# if(d.vehicle_frame_no){ }}
				<a class="layui-btn-a copyInfo" style="background:none;margin-left: 3px" data-name="{{d.vehicle_frame_no}}">
					<img src="../../img/copy.png" style="width: 25px;"/>
				</a>
				{{# } }}
			</span>
			<span class="order_massage_title">车辆品牌：{{d.brand_name?d.brand_name:''}}
				{{# if(d.brand_name){ }}
				<a class="layui-btn-a copyInfo" style="background:none;margin-left: 3px" data-name="{{d.brand_name}}">
					<img src="../../img/copy.png" style="width: 25px;"/>
				</a>
				{{# } }}
			</span>
			<span class="order_massage_title">车型名称：{{d.car_model?d.car_model:''}}
				{{# if(d.car_model){ }}
				<a class="layui-btn-a copyInfo" style="background:none;margin-left: 3px" data-name="{{d.car_model}}">
					<img src="../../img/copy.png" style="width: 25px;"/>
				</a>
				{{# } }}
			</span>
		</li>
		<li>
			<span class="order_massage_title">修理厂名称：{{d.repair_company_name?d.repair_company_name:''}}
				{{# if(d.repair_company_name){ }}
				<a class="layui-btn-a copyInfo" style="background:none;margin-left: 3px" data-name="{{d.repair_company_name}}">
					<img src="../../img/copy.png" style="width: 25px;"/>
				</a>
				{{# } }}
			</span>
			<span class="order_massage_title">修理厂电话：{{d.repair_company_phone?d.repair_company_phone:''}}
				{{# if(d.repair_company_phone){ }}
				<a class="layui-btn-a copyInfo" style="background:none;margin-left: 3px" data-name="{{d.repair_company_phone}}">
					<img src="../../img/copy.png" style="width: 25px;"/>
				</a>
				{{# } }}
			</span>
			<span class="order_massage_title">物流：{{d.logistics_name?d.logistics_name:''}}
				{{# if(d.logistics_name){ }}
				<a class="layui-btn-a copyInfo" style="background:none;margin-left: 3px" data-name="{{d.logistics_name}}">
					<img src="../../img/copy.png" style="width: 25px;"/>
				</a>
				{{# } }}
			</span>
			<span class="order_massage_title">报案号：{{d.service_no?d.service_no:''}}
				{{# if(d.service_no){ }}
				<a class="layui-btn-a copyInfo" style="background:none;margin-left: 3px" data-name="{{d.service_no}}">
					<img src="../../img/copy.png" style="width: 25px;"/>
				</a>
				{{# } }}
			</span>
		</li>
		<li>
			<span class="order_massage_title">经营主体：{{d.business_entity==='ht'?'鸿天':(d.business_entity==='cy'?'车友':'个体户')}}</span>
			<span class="order_massage_title">配件商名称：{{d.from_quotation_selection_json.add_company_name?d.from_quotation_selection_json.add_company_name:''}}
				{{# if(d.from_quotation_selection_json.add_company_name){ }}
				<a class="layui-btn-a copyInfo" style="background:none;margin-left: 3px" data-name="{{d.from_quotation_selection_json.add_company_name}}">
					<img src="../../img/copy.png" style="width: 25px;"/>
				</a>
				{{# } }}
			</span>
			<span class="order_massage_title">供货日期：{{d.sure_datetime?d.sure_datetime:''}}</span>
			<span class="order_massage_title">业务员：{{d.add_user_name?d.add_user_name:''}}</span>
		</li>
		<li>
			<span class="order_massage_title">采购配件总价：{{d.purchase_totall_price?d.purchase_totall_price:0}}</span>
			<span class="order_massage_title">采购运费+木架：{{d.purchase_freight?d.purchase_freight:0}}</span>
			<span class="order_massage_title">采购税金：{{d.purchase_tax?d.purchase_tax:0}}</span>
			<span class="order_massage_title">含税采购税金：{{d.purchase_totall_price_tax?d.purchase_totall_price_tax:0}}</span>
		</li>
		<li>
			<span class="order_massage_title">询价人商务费：{{d.loss_assessor_money}}
				{{# if(d.loss_assessor_state < 2){ }}
				<a class="layui-btn-a" style="margin-left: 15px;" id="payLossAssessor">支付</a>
				{{# } }}
			</span>
			<span class="order_massage_title">修理厂商务费：{{d.repair_company_money}}
				{{# if(d.repair_company_state < 2){ }}
				<a class="layui-btn-a" style="margin-left: 15px;" id="payRepairCompany">支付</a>
				{{# } }}
			</span>
			<span class="order_massage_title">总成本：{{d.total_cost?d.total_cost:0}}</span>
			<span class="order_massage_title">销售回款：{{d.returned_money?d.returned_money:0}}</span>
		</li>
		<li>
			<span class="order_massage_title">备注：{{d.common?d.common:''}}</span>
		</li>
		<!--<li>
			<span class="order_massage_title">销售配件总价：{{d.totall_price?d.totall_price:0}}</span>
			<span class="order_massage_title">销售运费+木架：{{d.freight?d.freight:0}}</span>
			<span class="order_massage_title">销售税金：{{d.tax}}</span>
			<span class="order_massage_title">销售总价：{{d.totall_price_tax}}</span>
		</li>-->
	</ul>
</script>
<script type="text/html" id="createSalesForm">
	<form lay-filter="createSalesForm" class="layui-form model-form">
		<input name="id" type="hidden"/>
		<div class="layui-form-item">
			<label class="layui-form-label"><span class="must">*</span>文件模版</label>
			<div class="layui-input-inline">
				<select name="business_entity" id="file_template" lay-search lay-verify="required" required>
					<option value="">请选择</option>
					<option value="ht">鸿天</option>
					<option value="cy">车友</option>
					<option value="personal">个体户</option>
				</select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label"><span class="must">*</span>带公章</label>
			<div class="layui-input-inline">
				<input type="radio" name="with_seal" value="1" title="是" checked>
				<input type="radio" name="with_seal" value="0" title="否">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label"><span class="must">*</span>运费</label>
			<div class="layui-input-inline">
				<input name="freight" type="number" class="layui-input" autocomplete="off" lay-verify="required" required />
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">运费支付方式</label>
			<div class="layui-input-inline">
				<input type="radio" name="freight_type" value="现付" title="现付" checked>
				<input type="radio" name="freight_type" value="到付" title="到付">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label"><span class="must">*</span>税率</label>
			<div class="layui-input-inline">
				<input name="tax_rate" type="number" class="layui-input" autocomplete="off" lay-verify="required" required />
			</div>
			<div class="layui-form-mid layui-word-aux">%</div>
		</div>
		<div class="layui-form-item text-center">
			<button class="layui-btn" type="button"  style="margin-right:2%;" ew-event="closeDialog">取消</button>
			<button class="layui-btn" lay-filter="createSalesFormSubmit" lay-submit>生成</button>
		</div>
	</form>
</script>
<script type="text/html" id="payForm">
	<form lay-filter="payForm" class="layui-form model-form">
		<input name="id" type="hidden"/>
		<input name="type" value="2" type="hidden"/>
		<input name="company_type" value="" type="hidden"/>
		<div class="layui-form-item">
			<label class="layui-form-label">应付金额</label>
			<div class="layui-input-inline">
				<input name="payable_amount" placeholder="请输入应付金额" type="number" class="layui-input" autocomplete="off" lay-verify="required" required/>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">已付金额</label>
			<div class="layui-input-inline">
				<input name="paid" placeholder="" type="text" class="layui-input" autocomplete="off" disabled/>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">实付金额</label>
			<div class="layui-input-inline">
				<input name="pay_money" placeholder="请输入本次打款实付金额" type="number" class="layui-input" autocomplete="off" lay-verify="required" required/>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">付款时间</label>
			<div class="layui-input-inline">
				<input type="text" name="pay_datetime" id="pay_datetime" placeholder="yyyy-MM-dd HH:mm:ss" lay-verify="required" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">付款方式</label>
			<div class="layui-input-block">
				<select name="pay_type" id="pay_type" lay-search lay-verify="required">
				</select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">付款凭证</label>
			<div class="layui-input-inline upload_icon" id="pic" style="width:50px;display: none">
				<img src="" id="img" class="imgs" style="width:100%; height: 100%;">
			</div>
			<p class="layui-btn" id='uploadfile'>
				<i class="layui-icon">&#xe67c;</i>
				<span>上传</span>
			</p>
			<input name="pic_url" id="pic_url" hidden/>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">备注</label>
			<div class="layui-input-inline">
				<input name="log_common" placeholder="备注" type="text" class="layui-input" autocomplete="off" maxlength="155" />
			</div>
		</div>
		<div class="layui-form-item text-center">
			<button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
			<button class="layui-btn" lay-filter="payFormSubmit" lay-submit>确定</button>
		</div>
	</form>
</script>
<!-- 表格里 运费税金修改-->
<script type="text/html" id="price">
	{{# if(d.id>0){ }}
	{{d[d.sale_use_quilty]}}
	{{# }else if(d.id == -3){ }}
	<div id="rate_price" lay-event='up_rate'>
		{{d.price}}%
	</div>
	<div id="up_rate" style="display: none">
		<input type="number" class="layui-input name_no_border" style="width: 100px" name="tax_rate" id="tax_rate" lay-filter="tax_rate" value="{{d.price>0 ?d.price:''}}">%
	</div>
	{{# } }}
</script>
<script type="text/html" id="totallprice">
	{{# if(d.id>0){ }}
	{{d.totall_price}}
	{{# }else if(d.id == -1){ }}
	<span class="peijian_totall">{{d.totall_price}}</span>
	{{# }else if(d.id == -3){ }}
	<div lay-event='up_tax_amount' id="tax_amount_price">
		{{d.totall_price}}
	</div>
	<div id="up_tax_amount" style="display: none">
		<input type="number" class="layui-input name_no_border price" style="width: 100px" name="tax_amount" id="tax_amount" value="{{d.totall_price>0?d.totall_price:''}}">
	</div>

	{{# }else if(d.id == -2){ }}
	<div lay-event='up_freight' id="freight_price">
		{{d.totall_price}}
	</div>
	<div id="up_freight" style="display: none">
		<input type="number" class="layui-input name_no_border" style="width: 100px" name="freight" id="freight" value="{{d.totall_price>0?d.totall_price:''}}">
	</div>
	{{# }else{ }}
	{{d.totall_price}}
	{{# } }}
</script>
<script type="text/html" id="picUrl">
	{{# if(d.pic_url.length>0){ }}
	{{# d.pic_url.forEach(function(item,index) { }}
	{{# if(item.slice(-3) === 'pdf'){ }}
	<div class="layui-input-inline upload_icon" style="height:30px;padding: 0 2px;">
		<a target="_blank" rel="nofollow" href='{{item}}'><i class="layui-icon">&#xe621;</i>附件{{index+1}}</a>
	</div>
	{{# }else{ }}
	<div class="layui-input-inline upload_icon" style="height:30px;padding: 0 2px;">
		<img src="{{item}}" class="imgs" style="width:100%; height: 100%;">
	</div>
	{{# } }}
	{{# })} }}
</script>

<script type="text/html" id="invoiceLogBar">
	{{# if(d.state == 1 && (window.user_info.role_id == 1 || window.user_info.role_id == 3)){ }}
	<a class="layui-btn-a" lay-event="edit">修改</a>
	{{#} }}
</script>
<script type="text/html" id="moneyLogBar">
	{{# if(window.user_info.role_id == 1 || window.user_info.role_id == 3){ }}
	<a class="layui-btn-a" lay-event="edit">修改</a>
	<a class="layui-btn-a btn-red" lay-event="del">删除</a>
	{{#} }}
</script>
<!-- 收款表单弹窗 -->
<script type="text/html" id="receiveMoneyForm">
	<form lay-filter="receiveMoneyForm" class="layui-form model-form">
		<input name="id" type="hidden"/>
		<div class="layui-form-item">
			<label class="layui-form-label">收款金额</label>
			<div class="layui-input-block">
				<input name="account" placeholder="请输入收款金额" type="number" class="layui-input" autocomplete="off" lay-verify="required" required/>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">收款时间</label>
			<div class="layui-input-block">
				<input type="text" name="pay_datetime" id="pay_datetime" placeholder="yyyy-MM-dd HH:mm:ss" lay-verify="required" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">收款状态</label>
			<div class="layui-input-block">
				<input type="radio" name="returned_money_state" value="1" title="部分已收" checked>
				<input type="radio" name="returned_money_state" value="2" title="全部已收">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">收款方式</label>
			<div class="layui-input-block">
				<select name="pay_type" id="pay_type" lay-search lay-verify="required">
				</select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">备注</label>
			<div class="layui-input-block">
				<input name="common" placeholder="备注" type="text" class="layui-input"
					   maxlength="155" autocomplete="off"/>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">收款凭证</label>
			<div class="layui-input-block">
				<div class="layui-upload">
					<button type="button" class="layui-btn" id="test2"><i class="layui-icon">&#xe67c;</i>上传</button>
					<blockquote>
						<div class="layui-upload-list"  style="margin: 0px 10px 30px;" id="demo2"></div>
					</blockquote>
				</div>
			</div>
		</div>
		<div class="layui-form-item text-center">
			<button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
			<button class="layui-btn" lay-filter="receiveMoneyFormSubmit" lay-submit>确认</button>
		</div>
	</form>
</script>
<script type="text/html" id="invoiceForm">
	<form lay-filter="invoiceForm" class="layui-form model-form">
		<input name="id" type="hidden"/>
		<div class="layui-form-item">
			<label class="layui-form-label">开票时间</label>
			<div class="layui-input-block">
				<input type="text" name="invoice_datetime" id="invoice_datetime" placeholder="yyyy-MM-dd HH:mm:ss" lay-verify="required" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">发票状态</label>
			<div class="layui-input-block">
				<input type="radio" name="state" value="1" title="正常" checked>
				<input type="radio" name="state" value="2" title="红冲">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">备注</label>
			<div class="layui-input-block">
				<input name="common" placeholder="备注" type="text" class="layui-input"
					   maxlength="155" autocomplete="off"/>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">开票凭证</label>
			<div class="layui-input-block">
				<div class="layui-upload">
					<button type="button" class="layui-btn" id="test2"><i class="layui-icon">&#xe67c;</i>上传</button>
					<blockquote>
						<div class="layui-upload-list"  style="margin: 0px 10px 30px;" id="demo2"></div>
					</blockquote>
				</div>
			</div>
		</div>
		<div class="layui-form-item text-center">
			<button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
			<button class="layui-btn" lay-filter="invoiceFormSubmit" lay-submit>确定</button>
		</div>
	</form>
</script>
<script type="text/javascript" src="../../js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="../../js/jquery.cookie.js"></script>
<script type="text/javascript" src="../../common/layui/layui2.js"></script>
<script type="text/javascript" src="../../js/common.js"></script>
<script type="text/javascript" src="../../js/main.js"></script>
<script type="text/javascript" src="../../jsplug/xm-select.js"></script>
<script>
	var id = GetQueryString('id');
	var user_info =  JSON.parse(getSessionStorage('uanme'));
	layui.use(['jquery','form','table','laytpl','admin','upload','laydate'], function(){
		var $ = layui.jquery;
		var form = layui.form;
		var table = layui.table;
		var laytpl = layui.laytpl;
		var admin = layui.admin;
		var upload = layui.upload;
		var laydate = layui.laydate;

		var tableIns
		var DISABLED = 'layui-btn-disabled';
		form.render();

		var detail_data = [];
		var detailList = [];
		var scrollPosition = {
			table: 0, // 表格容器的滚动位置
			parent: 0 // 父容器的滚动位置（如果适用）
		};
		var resultImage = [];
		var file_index = 0;
		getInfo();
		function getInfo() {
			send_req('Sales/getDetailInfo', {id: id}, function (data) {
				detail_data = data;
				// 渲染车辆信息详情
				var orderDetail = document.getElementById('orderDetail');
				var getTpl = orderDetail.innerHTML;
				var view = document.getElementById('orderView');
				laytpl(getTpl).render(detail_data, function(html){
					view.innerHTML = html;
				});

				detailList = data.detail_sales;
				tableIns = table.render({
					elem: '#infoTable',
					data: detailList, // 使用本地数据源
					limit:100,
					page: false, // 如果不需要分页，设置为false
					cols: [[
						{title: '序号',width: 60, templet: function(d){
								if (d.id > 0) {
									return d.LAY_INDEX; // LAY_INDEX 是 Layui 表格为每行自动生成的索引
								}else if(d.id < -1){
									return d.LAY_INDEX - 1;
								} else {
									return '';
								}
							}},
						{field: 'eq_name',title: '零件名称', templet: function(d){
								if (d.id == -1) {
									return "<span class='peijian_totall'>"+d.eq_name+"</span>";
								} else {
									return d.eq_name;
								}
							}},
						{field: 'oe_no', title: 'OE码'},
						{field: 'num', title: '数量'},
						{title: '单价',templet:'#price'},
						{field: 'totall_price', title: '金额',templet:'#totallprice'},
						{ title: '品质',templet:function (d) {
								return d.sale_use_quilty?makeSureQuilty[d.sale_use_quilty]:''
							}},
						{field: 'common', title: '备注'},
						{field: 'state', title: '状态',templet:function (d) {
								if(d.state === ''){
									return '';
								}else if (d.state === 4) {
									return "<span style='color: #fd1e11'>退件</span>";
								}else{
									return "<span style='color: #3eff3a'>正常</span>";
								}
							}},
					]], // 动态生成的列定义
					done: function (res, curr, count) {
						var tableContainer = document.querySelector('#infoTable').parentElement;
						tableContainer.scrollTop = scrollPosition.table;
						// 如果有父容器，也恢复父容器的滚动位置
						if (scrollPosition.parent !== undefined) {
							document.documentElement.scrollTop = scrollPosition.parent || document.body.scrollTop;
						}
					}
				});

				$('#payLossAssessor').on('click', function (event) {
					event.preventDefault(); // 阻止默认行为
					var data = {'id':id,'company_type':3};
					if(detail_data.loss_assessor_money){
						data.payable_amount = detail_data.loss_assessor_money;
						data.paid = detail_data.loss_assessor_pay;
					}
					payMoney(data); // 调用实际的方法
					return false;
				});
				$('#payRepairCompany').on('click', function (event) {
					event.preventDefault(); // 阻止默认行为
					var data = {'id':id,'company_type':4};
					if(detail_data.repair_company_money){
						data.payable_amount = detail_data.repair_company_money;
						data.paid = detail_data.repair_company_pay;
					}
					payMoney(data); // 调用实际的方法
					return false;
				});

				$(document).on('blur', '#up_rate', function() {
					var tax_rate = $('#tax_rate').val();
					if (isNaN(tax_rate) || tax_rate <= 0) {
						$(this).val(0); // 如果值是'0'，则将其清空
						tax_rate = 0;
						form.render();
					}
					var up_data = {
						"id":id,
						"tag_name":"tax_rate",
						"value":tax_rate
					}
					editInfo(up_data);
					return false;
				});

				$(document).on('blur', '#up_freight', function() {
					var freight = $('#freight').val();
					if (isNaN(freight) || freight <= 0) {
						$(this).val(0); // 如果值是'0'，则将其清空
						freight = 0;
						form.render();
					}
					var up_data = {
						"id":id,
						"tag_name":"freight",
						"value":freight
					}
					editInfo(up_data);
					return false;
				});

				$(document).on('blur', '#up_tax_amount', function() {
					var newValue = $('#tax_amount').val();
					if (isNaN(newValue) || newValue <= 0) {
						$(this).val(0); // 如果值是'0'，则将其清空
						newValue = 0;
						form.render();
					}
					var up_data = {
						"id":id,
						"tag_name":"tax_amount",
						"value":newValue
					}
					editInfo(up_data);
					return false;
				});

				$('.copyInfo').on('click', function (event) {
					event.preventDefault(); // 阻止默认行为
					var dataName = $(this).data('name'); // 或者使用 attr('data-name')
					copyToClipboard(dataName);
					return false;
				});
			});
		}

		table.on('tool(infoTable)', function (obj) {
			var data = obj.data;
			if(obj.event === 'up_rate'){
				$('#rate_price').hide();
				$('#up_rate').show();
			}else if(obj.event === 'up_freight'){
				$('#freight_price').hide();
				$('#up_freight').show();
			}else if(obj.event === 'up_tax_amount'){
				$('#tax_amount_price').hide();
				$('#up_tax_amount').show();
			}
		});

		table.render({
			elem: '#moneyLog',
			method:'POST',
			url: access_baseurl+'Sales/getAccountLog', // 使用本地数据源
			where:{id: id},
			page: true, // 如果不需要分页，设置为false
			limit:3,
			limits:[3,6],
			cols: [[
				{field: 'account',title: '付款金额',width: 120},
				{field: 'pay_type', title: '付款方式',width: 150},
				{field: 'pay_datetime', title: '付款时间',width: 180},
				{field: 'company_type', title: '类型',width: 100,templet: function (d) {
						if(d.company_type === 1){
							return '客户'
						}else if(d.company_type === 3){
							return '询价人'
						}else if(d.company_type === 4){
							return '修理厂'
						}
					}},
				{field: 'common', title: '备注'},
				{title: '付款凭证',templet:"#picUrl"},
				{align: 'left', toolbar: '#moneyLogBar', title: '操作',width:120}
			]], // 动态生成的列定义
			done: function (res, curr, count) {
			}
		});

		table.render({
			elem: '#sendGoodsLog',
			method:'POST',
			url: access_baseurl+'Sales/getSendGoodsLog', // 使用本地数据源
			where:{id: id},
			page: true, // 如果不需要分页，设置为false
			limit:3,
			limits:[3,6],
			cols: [[
				{field: 'send_datetime',title: '发货时间'},
				// {field: 'send_goods_no',title: '发货单号'},
				{field: 'common',title: '备注'},
				{ title: '发货凭证',templet:"#picUrl",width: 130},
			]], // 动态生成的列定义
			done: function (res, curr, count) {
			}
		});

		table.render({
			elem: '#invoiceLog',
			method:'POST',
			url: access_baseurl+'Sales/getInvoiceLog', // 使用本地数据源
			where:{id: id},
			page: true, // 如果不需要分页，设置为false
			limit:3,
			limits:[3,6],
			cols: [[
				{field: 'invoice_datetime',title: '开票时间'},
				{title: '状态',width: 80,templet: function (d) {
					if(d.state === 1){
						return '正常'
					}else {
						return "<span style='color: #fd1e11'>已红冲</span>";
					}
				}},
				{field: 'common',title: '备注'},
				{title: '开票凭证',templet:"#picUrl",width: 130},
				{align: 'left', toolbar: '#invoiceLogBar', title: '操作',width:80}
			]], // 动态生成的列定义
			done: function (res, curr, count) {
			}
		});


		$('#createFile').click(function () {
			admin.open({
				type: 1,
				area: '500px',
				offset: '65px',
				title: '生成供货凭证',
				content: $('#createSalesForm').html(),
				success: function (layero, index) {
					// console.log(layero)
					$(layero).children('.layui-layer-content').css('overflow', 'visible');
					form.val('createSalesForm',detail_data);
					form.render();
					// 生成报价单
					form.on('submit(createSalesFormSubmit)', function (d) {
						send_req('Sales/createSales', d.field, function (res) {
							layer.closeAll('page');
							layer.closeAll('loading');
							location.href = access_baseurl + 'Inquiry/createSalesPurchaseFile.html?id='+id+'&with_seal='+d.field.with_seal+'&type=sales'+'&file_template='+d.field.business_entity;
						},false);
						getInfo()
						return false;
					})
				}
			})
			return false;
		});

		function payMoney(data){
			admin.open({
				type: 1,
				area: '500px',
				offset: ['65px','450px'],
				title: data.company_type === 3?'支付询价人商务费':'支付修理厂商务费',
				content: $('#payForm').html(),
				success: function (layero, index) {
					// console.log(layero)
					$(layero).children('.layui-layer-content').css('overflow', 'visible');
					uploadImg('paypic','#uploadfile',function (data) {
						$('#pic_url').val(data.file);
						$('#pic').show();
						$('#img').attr('src',data.file)
					})

					form.val('payForm',data);
					var payStr = getPayType();
					$('#pay_type').html(payStr);

					laydate.render({
						elem: '#pay_datetime'
						,trigger: 'click',
						type: 'datetime', // 设置类型为 datetime
						format: 'yyyy-MM-dd HH:mm:ss', // 设置显示格式
						value: new Date()
					});
					form.render();
				}
			})
			return false;
		}

		form.on('submit(payFormSubmit)', function(d){
			// 增加样式
			$('.site-demo-active').addClass(DISABLED);
			// 增加属性
			$('.site-demo-active').attr('disabled', 'disabled');
			console.log(d.field)
			layer.load(); //上传loading
			send_req('Sales/payBusinessExpenses',d.field,function (res) {
				layer.msg('提交成功', {icon: 1, time:1500}, function () {
					location.replace('./detail.html?id=' + id);
					layer.closeAll('loading');
					$('.site-demo-active').removeClass('layui-btn-disabled');
					$('.site-demo-active').removeAttr('disabled');
				});
			},false)
			return false;
		});

		function getPayType(){
			var pay_type;
			send_req('Dictionary/getListByType', {type:1}, function (data) {
				pay_type = data.pay_type;
			},false);
			var payStr = '<option value="">全部</option>';
			for(var i=0;i<pay_type.length;i++){
				payStr+='<option value="'+pay_type[i].name+'">'+pay_type[i].name+'</option>';
			}
			return payStr;
		}
		function uploadImg(dicName='pic',elem='#uploadfile',succfun){
			upload.render({
				elem: elem //绑定元素
				,url: access_baseurl + 'File/uploadImg' //上传接口
				,data:{
					'dicName':dicName,
				}
				,accept:'images'
				,acceptMime:'image/*'
				,done: function(res){
					//上传完毕回调
					if(res.success === true){
						succfun(res.obj);
					}else {
						layer.closeAll('loading');
						layer.msg(res.msg, { icon: 2, anim: 6 });
						$('.site-demo-active').removeClass('layui-btn-disabled');
						$('.site-demo-active').removeAttr('disabled');
					}
				}
				,error: function(){
					//请求异常回调
				}
			});
		}

		function reloadTable(info='') {
			var tableContainer = document.querySelector('#infoTable').parentElement;
			scrollPosition.table = tableContainer.scrollTop; // 保存表格容器的滚动位置
			// 如果有父容器，保存父容器的滚动位置
			scrollPosition.parent = document.documentElement.scrollTop || document.body.scrollTop;
			// 表格重载
			tableIns.reload({
				data: info?info:table.cache['infoTable']
			});
		}

		function editInfo(data){
			layer.load();
			send_req('Sales/editFrightOrRate',data,function (res) {
				layer.msg('修改成功', {icon: 1}, function () {
					reloadTable(res.detail_sales);
					if(data.tag_name === 'tax_rate'){
						$('#up_rate').hide();
						$('#rate_price').show();
					}else if(data.tag_name === 'tax_amount'){
						$('#up_tax_amount').hide();
						$('#tax_amount_price').show();
					}else{
						$('#up_freight').hide();
						$('#freight_price').show();
					}
					form.render();
					layer.closeAll('loading');
				});
			},false)
		}

		table.on('tool(moneyLog)', function (obj) {
			var data = obj.data;
			if(obj.event === 'edit') { // 删除
				resultImage = [];
				showReceiveMoneyModel(data)
			}else if(obj.event === 'del') { // 删除
				layer.confirm('确定要删除吗？', {
					skin: 'layui-layer-admin'
				}, function (i) {
					layer.close(i);
					layer.load(2);
					send_req('Sales/delMoenyLog', {id:data.id,bus_inquiry_id:id}, function (res) {
						layer.closeAll('loading');
						layer.msg('删除成功', {icon: 1});
						getInfo();
						table.reload('moneyLog');
					});
					return false;
				});
			}
		});

		function showReceiveMoneyModel(form_data){
			admin.open({
				type: 1,
				area: '600px',
				offset: ['65px','450px'],
				title: '收款',
				content: $('#receiveMoneyForm').html(),
				success: function (layero, index) {
					$(layero).children('.layui-layer-content').css('overflow', 'visible');
					uploadImgs()
					laydate.render({
						elem: '#pay_datetime'
						,trigger: 'click',
						type: 'datetime', // 设置类型为 datetime
						format: 'yyyy-MM-dd HH:mm:ss', // 设置显示格式
						value: new Date()
					});
					var payStr = getPayType();
					$('#pay_type').html(payStr);
					resultImage = form_data.pic_url;
					if(resultImage.length>0){
						for (var i=0; i<resultImage.length;i++){
							$('#demo2').append( '<div class="file-iteme">' +
									'<div class="handle" id='+i+'><i class="layui-icon layui-icon-delete"></i></div>' +
									'<img class="imgs" style="width: 100px;height: 100px;" src='+ resultImage[i] +'>' +
									'<div class="info"></div>' +
									'</div>');
						}
					}

					form.val('receiveMoneyForm',form_data);
					form.render();
				}
			})
		}
		form.on('submit(receiveMoneyFormSubmit)', function (d) {
			d.field.pic_url = resultImage.length>0?resultImage.join(','):'';
			d.field.bus_inquiry_id = id;
			layer.load(2);
			send_req('Sales/editReceiveMoeny', d.field, function (res) {
				layer.closeAll('page');
				layer.closeAll('loading');
				layer.msg('操作成功', {icon: 1});
				getInfo();
				table.reload('moneyLog');
			},false);
			return false;
		});

		table.on('tool(invoiceLog)', function (obj) {
			var data = obj.data;
			if(obj.event === 'edit') { // 删除
				resultImage = [];
				showInvoiceModel(data);
			}
		});
		function showInvoiceModel(form_data){
			admin.open({
				type: 1,
				area: '600px',
				offset: ['65px','450px'],
				title: '开票',
				content: $('#invoiceForm').html(),
				success: function (layero, index) {
					$(layero).children('.layui-layer-content').css('overflow', 'visible');
					form.val('invoiceForm',form_data);
					uploadImgs('file')
					laydate.render({
						elem: '#invoice_datetime'
						,trigger: 'click',
						type: 'datetime', // 设置类型为 datetime
						format: 'yyyy-MM-dd HH:mm:ss', // 设置显示格式
						value: new Date()
					});
					resultImage = form_data.pic_url;
					if(resultImage.length>0){
						for (var i=0; i<resultImage.length;i++){
							$('#demo2').append( '<div class="file-iteme">' +
									'<div class="handle" id='+i+'><i class="layui-icon layui-icon-delete"></i></div>' +
									'<a target="_blank" rel="nofollow" href="javascript:;" src='+ resultImage[i] +'><i class="layui-icon" style="font-size:28px;color: #ff9900;font-weight: bold">&#xe621;</i></a>' +
									'<div class="info">附件' + (i+1) + '</div>' +
									'</div>');
						}
					}
					form.render();
					return false;
				}
			})
		}

		form.on('submit(invoiceFormSubmit)', function (d) {
			d.field.pic_url = resultImage.length>0?resultImage.join(','):'';
			d.field.bus_inquiry_id = id;
			layer.load(2);
			send_req('Sales/editInvoice', d.field, function (res) {
				layer.closeAll('page');
				layer.closeAll('loading');
				layer.msg('操作成功', {icon: 1});
				table.reload('invoiceLog');
			},false);
			return false;
		});

		$(document).on("mouseenter mouseleave", ".file-iteme", function(event){
			if(event.type === "mouseenter"){
				//鼠标悬浮
				$(this).children(".info").fadeIn("fast");
				$(this).children(".handle").fadeIn("fast");
			}else if(event.type === "mouseleave") {
				//鼠标离开
				$(this).children(".info").hide();
				$(this).children(".handle").hide();
			}
		});
		// 删除图片
		$(document).on("click", ".file-iteme .handle", function(event){
			var index = $(this).context.id;
			$(this).parent().remove();
			resultImage.splice(index,1);
			return false;
		});

		$(document).off('click','.imgs').on("click", ".imgs", function(event){
			var imgSrc = $(this).attr("src");
			window.parent.open_pic_url(imgSrc);
			return false;
		});

		function uploadImgs(type='images'){
			upload.render({
				elem: '#test2'
				,url: access_baseurl + 'File/uploadImg' //改成您自己的上传接口
				,multiple: true
				,accept:type=='images'?'images':'file'
				,acceptMime:type=='images'?'image/*':'image/*, application/pdf'
				,before: function(obj){
					if(resultImage.length>=9){
						layer.msg('最多可上传9张', {icon: 2});
						return false;
					}
					//预读本地文件示例，不支持ie8
					obj.preview(function(index, file, result){
						if(type=='images'){
							$('#demo2').append( '<div class="file-iteme">' +
									'<div class="handle" id='+file_index+'><i class="layui-icon layui-icon-delete"></i></div>' +
									'<img class="imgs" style="width: auto;height: 50px;" src='+ result +'>' +
									'<div class="info">' + file.name + '</div>' +
									'</div>');
						}else{
							$('#demo2').append( '<div class="file-iteme">' +
									'<div class="handle" id='+file_index+'><i class="layui-icon layui-icon-delete"></i></div>' +
									'<a target="_blank" rel="nofollow" href="javascript:;" src='+ result +'><i class="layui-icon" style="font-size:28px;color: #ff9900;font-weight: bold">&#xe621;</i></a>' +
									'<div class="info">' + file.name + '</div>' +
									'</div>');
						}

					});
				}
				,done: function(res){
					//上传完毕
					if(res.success){
						file_index += 1;
						resultImage.push(res.obj.file);
						return false;
					}
				}
			});
		}
		// 复制文本到剪贴板的函数
		function copyToClipboard(text) {
			navigator.clipboard.writeText(text).then(function() {
				layer.msg('复制成功');
			}, function(err) {
				console.error('未能复制文本: ', err);
				layer.msg('复制失败，请手动复制');
			});
		}
	});
</script>
</body>
</html>